<template>
  <el-card shadow="hover">
    <!--    查询条件部分-->
    <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">查询条件</span></el-divider>
    <div>
      <el-form label-position="right" label-width="100px">

        <el-row :gutter="24">
          <el-col :span="9">
            <el-form-item label="用户信息：">
              <el-input v-model="queryParma.yhxx" size="small" placeholder="请输入查询的用户信息" />
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="角色名称：">
              <el-input v-model="queryParma.userRoleName" size="small" placeholder="请输入查询的角色名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6" align="right">
            <el-button type="primary" @click="getAllUserRoleDataPageHandler">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!--    查询查询部分-->
    <div>
      <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">用户角色信息管理</span></el-divider>
      <el-table
        :data="dataSource"
        :border="true"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          :index="indexMethod"
          label="#"
          width="50"
          fixed="left"
          align="center"
        />
        <el-table-column
          label="用户账号"
          header-align="center"
          prop="userName"
        />
        <el-table-column
          label="用户昵称"
          header-align="center"
          prop="userInfoName"
        >
          <template slot-scope="{row}">
            <span v-if="row.userInfoName !== null">{{ row.userInfoName }}</span>
            <span v-else>该账户尚未完善个人信息</span>
          </template>
        </el-table-column>
        <el-table-column
          label="角色名称"
          header-align="center"
          align="center"
          prop="roleName"
        >
          <template slot-scope="{row}">
            <span v-if="row.roleName !== null">{{ row.roleName }}</span>
            <span v-else>该账户尚未绑定角色信息</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">

            <el-popover
              placement="right"
              width="350"
              trigger="click"
              style="margin: 0 auto"
            >
              <div>
                <span style="display: inline-block">角色：</span>
                <div style="display: inline-block">
                  <el-select v-model="scope.row.roleCode" :value="scope.row.roleCode" placeholder="请选择楼号">
                    <el-option
                      v-for="item in roleList"
                      :key="item.roleCode"
                      :label="item.roleName"
                      :value="item.roleCode"
                    />
                  </el-select>
                </div>
                <div style="display: inline-block;margin-left: 10px">
                  <div>
                    <el-button type="primary" @click="handlerSaveChange(scope.row)">保存</el-button>
                  </div>
                </div>
              </div>

              <el-button
                v-if="scope.row.roleName !== null"
                slot="reference"
                size="mini"
                type="danger"
                plain
                @click="handleChangeRoleBinding(scope.$index, scope.row)"
              >更改用户角色绑定
              </el-button>
              <el-button
                v-else
                slot="reference"
                size="mini"
                type="primary"
                plain
                @click="handleChangeRoleBinding(scope.$index, scope.row)"
              >角色绑定
              </el-button>
            </el-popover>
          </template>

        </el-table-column>
      </el-table>
      <div align="center">
        <div style="display: inline-block;width: 60px;height:60px;line-height: 60px;text-align: center;font-size: 15px">
          <span style="text-align: center;line-height: 60px;width: 60px;height:60px;">共{{ dataTotals }}条</span>
        </div>
        <div style="display: inline-block">
          <el-pagination
            :current-page.sync="queryParma.pageIndex"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryParma.pageSize"
            layout="total, sizes, prev,next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
const { getAllUserDtoPage } = require('@/api/user')
const { Message } = require('element-ui')
const { getRoles } = require('@/api/role')
const { modifyUserRole } = require('@/api/userrole')
export default {
  name: 'Index',
  data() {
    return {
      dataSource: [], // 数据源
      roleList: [], // 角色列表
      loading: false,
      dataTotals: 0,
      searchloding: false,
      queryParma: { // 查询参数对象
        pageIndex: 1, // 当前页
        pageSize: 10, // 当前页大小
        yhxx: '', // 用户信息
        userRoleName: '' // 角色名称
      },
      xxxTypeList: [
        {
          key: '01',
          value: 'xxx类型1'
        },
        {
          key: '02',
          value: 'xxx类型2'
        }
      ]
    }
  },
  mounted() {
    this.getAllUserRoleDataPageHandler() // 获取用户角色信息列表
    this.getAllRolesListHandler() // 获取所有角色信息
  },
  methods: {
    handlerSaveChange(row) {
      this.modifyUserRoleHandler(row)
    },
    modifyUserRoleHandler(row) {
      this.modifyUserRole(row)
    },
    async modifyUserRole(row) {
      const res = await modifyUserRole(row.userCode, row.roleCode)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'success',
          type: 'success',
          duration: 2 * 1000
        })
        this.getAllUserRoleDataPageHandler()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    resetForm() {
      this.queryParma.userRoleName = ''
      this.queryParma.yhxx = ''
      this.queryParma.pageSize = 10
      this.queryParma.pageIndex = 1
    },
    handleSizeChange(val) {
      this.queryParma.pageSize = val
      this.getAllUserRoleDataPageHandler()
    },
    handleCurrentChange(val) {
      this.queryParma.pageIndex = val
      this.getAllUserRoleDataPageHandler()
    },
    getAllRolesListHandler() {
      this.getAllRolesList()
    },
    async getAllRolesList() {
      const res = await getRoles()
      const data = res.data.main
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        this.roleList = data
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    getAllUserRoleDataPageHandler() {
      this.getAllUserRoleDataPage()
    },
    async getAllUserRoleDataPage() {
      this.loading = true
      const res = await getAllUserDtoPage(this.queryParma)
      const data = res.data.main.dataArray
      const resp = res.data.respOutMsgHeader
      const totals = res.data.main.totalsData
      const currentPage = res.data.main.currentPage
      if (resp.respCode === 200) {
        setTimeout(() => {
          this.searchloding = false
          this.loading = false
        }, 500)
        this.dataSource = data
        this.dataTotals = totals
        this.queryParma.pageIndex = currentPage
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.loading = false
      }
    },
    handleChangeRoleBinding(index, row) {
      console.log(row)
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    handleDelete(index, row) {
      console.log(index, row)
    },
    indexMethod(index) {
      return index + 1 + (this.queryParma.pageIndex - 1) * this.queryParma.pageSize
    }
  }
}
</script>

<style scoped>

</style>
